package org.lgiavedoni.android.franq.core.question.imp;

import java.util.List;

import lombok.Data;

import org.lgiavedoni.android.franq.core.contact.Contact;
import org.lgiavedoni.android.franq.core.message.MessageIn;
import org.lgiavedoni.android.franq.core.question.Question;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;
import org.lgiavedoni.android.franq.service.phone.ContactService;
import org.lgiavedoni.android.franq.util.collection.CollectionUtils;

@Data
public class ContactQuestion extends Question {

	private final String text;

	@Override
	public String getQuestionText() {
		return text;
	}

	protected List<Contact> getContacts(MessageIn messageIn) {
		return ContactService.inst.getContactsByName(messageIn.getText());
	}

	@Override
	public boolean process(MessageIn messageIn) {
		List<Contact> lContacts = getContacts(messageIn);
		if (lContacts == null || lContacts.size() == 0) {
			CommunicationService.inst.sendDefaultTextMessage("Sorry, there aren't any contact with that name. Please try again");
			return false;
		}
		if (lContacts.size() > 1) {
			CommunicationService.inst.sendDefaultTextMessage("Sorry, there are more than one contact for that name. [" + CollectionUtils.toString(lContacts, ",") + "]");
			return false;
		}
		if (lContacts.size() == 1) {
			setResult(lContacts.get(0));
			return true;
		}
		return false;
	}

	@Override
	public boolean equals(Question q) {
		return false;
	}

}
